from flask import Blueprint, render_template
from database import get_db

# 创建看板蓝图
board_general_bp = Blueprint('board_general', __name__, url_prefix='/board')

# 综合看板路由
@board_general_bp.route('/general')
def index():
    db = get_db()
    
    # 获取年度碳排放数据
    annual_emission_data = db.execute(
        'SELECT year, total_emission_include_energy_tco2e as total_emission FROM report_annual_emission ORDER BY year'
    ).fetchall()
    
    # 获取不同类别碳排放占比
    category_emission_data = [
        {'category': '化石燃料燃烧', 'emission': db.execute('SELECT SUM(fossil_fuel_co2_tco2e) FROM report_annual_emission').fetchone()[0] or 0},
        {'category': '电力消耗', 'emission': db.execute('SELECT SUM(purchased_electricity_tco2e) FROM report_annual_emission').fetchone()[0] or 0},
        {'category': '热力消耗', 'emission': db.execute('SELECT SUM(purchased_heat_tco2e) FROM report_annual_emission').fetchone()[0] or 0},
        {'category': '工业过程', 'emission': db.execute('SELECT SUM(process_co2_tco2e) FROM report_annual_emission').fetchone()[0] or 0},
        {'category': '其他过程', 'emission': db.execute('SELECT SUM(process_hfcs_tco2e + process_pfcs_tco2e + process_sf6_tco2e) FROM report_annual_emission').fetchone()[0] or 0},
    ]
    
    # 获取能源消耗统计
    energy_consumption_data = db.execute(
        'SELECT fuel_type as energy_type, consumption FROM report_fuel_consumption'
    ).fetchall()
    
    # 获取减排目标完成情况 - 暂时注释，使用虚拟数据
    # emission_reduction_data = db.execute(
    #     'SELECT year, target, actual FROM emission_reduction ORDER BY year'
    # ).fetchall()
    
    # 虚拟减排目标完成情况数据
    emission_reduction_data = [
        {'year': 2020, 'target': 1000, 'actual': 950},
        {'year': 2021, 'target': 950, 'actual': 920},
        {'year': 2022, 'target': 900, 'actual': 880},
        {'year': 2023, 'target': 850, 'actual': 860},
        {'year': 2024, 'target': 800, 'actual': 780}
    ]
    
    # 格式化数据为ECharts所需格式
    annual_emission = {
        'years': [str(item['year']) for item in annual_emission_data] if annual_emission_data else [],
        'emissions': [float(item['total_emission']) if item['total_emission'] is not None else 0.0 for item in annual_emission_data] if annual_emission_data else []
    }
    
    category_emission = {
        'categories': [item['category'] for item in category_emission_data] if category_emission_data else [],
        'emissions': [float(item['emission']) if item['emission'] is not None else 0.0 for item in category_emission_data] if category_emission_data else []
    }
    
    energy_consumption = {
        'types': [item['energy_type'] for item in energy_consumption_data] if energy_consumption_data else [],
        'consumptions': [float(item['consumption']) if item['consumption'] is not None else 0.0 for item in energy_consumption_data] if energy_consumption_data else []
    }
    
    emission_reduction = {
        'years': [str(item['year']) for item in emission_reduction_data] if emission_reduction_data else [],
        'targets': [float(item['target']) if item['target'] is not None else 0.0 for item in emission_reduction_data] if emission_reduction_data else [],
        'actuals': [float(item['actual']) if item['actual'] is not None else 0.0 for item in emission_reduction_data] if emission_reduction_data else []
    }
    
    return render_template('board_general.html', 
                          annual_emission=annual_emission,
                          category_emission=category_emission,
                          energy_consumption=energy_consumption,
                          emission_reduction=emission_reduction)